<?php

namespace Addons\Scene\Controller;
use Home\Controller\AddonsController;

class SceneController extends AddonsController{
    function create(){
        $view = array();
        $token = get_token();
        $access_token = get_access_token ( $token );
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $access_token;
        $postdata['action_name'] = "QR_LIMIT_SCENE";

        $mdres = M('com_md')->field('id, com_md_code, ticket, url')->select();
        foreach($mdres as $mditem){
            if($mditem['ticket']){
                $view[] = '编号:' . $mditem['com_md_code'] . '门店已有二维码';
            }
            else{
                $postdata['action_info'] = array('scene'=>array('scene_id'=>$mditem['id']));
                $res = json_decode(JsonPost($url, json_encode($postdata)), true);
                if($res['ticket']){
                    M('com_md')->where(array('id'=>$mditem['id']))->save($res);
                    $view[] = '编号:' . $mditem['com_md_code'] . '门店生成永久二维码成功';
                }
                else{
                    $view[] = '编号:' . $mditem['com_md_code'] . '门店生成永久二维码失败';
                }
            }
        }

        $this->assign('createresult', $view);
        $this->display();
//        $this->ajaxReturn($view);
    }
    function showqrcode(){
        $this->display();
    }
    function showmdlist(){
        $smdcode = session('gy_mdcode');
        if(empty($smdcode)){
            $param['tourl'] = 'showmdlist';
            redirect ( addons_url ( 'WXCoupon://WXCoupon/mdlogin', $param ) );
        }
        else{
            $mdinfo = M('com_md')->field('com_jc,com_zip,com_md_type01')->where(array('com_md_code'=>$smdcode))->find();
            if($mdinfo['com_md_type01'] ==3){
                $mdlist = M('com_md')
                    ->table('wp_com_md md')
                    ->join('LEFT JOIN wp_follow follow ON md.id = follow.mdid')
                    ->group('id')
                    ->order('fcount desc')
                    ->field('md.id, md.com_md_code as code, md.com_zip_name as name, count(follow.mdid) as fcount')
                    ->where("md.com_jc = '" . $mdinfo['com_jc'] . "' and (md.com_md_type01 != 4 or md.com_md_type01 is null)")
                    ->select();
                $mdcount = M('follow')
                    ->table('wp_follow follow')
                    ->join('wp_com_md md on md.id = follow.mdid')
                    ->field('count(mdid) as count')
                    ->where("mdid != 0 and md.com_jc = '" . $mdinfo['com_jc'] . "' and (md.com_md_type01 != 4 or md.com_md_type01 is null)")
                    ->find();
                $this->assign('mdlist', $mdlist);
                $this->assign('mdcount', $mdcount['count']);
                $this->display();
            }
            else if($smdcode =='800000'){
                $mdlist = M('com_md')
                    ->table('wp_com_md md')
                    ->join('LEFT JOIN wp_follow follow ON md.id = follow.mdid')
                    ->group('id')
                    ->order('fcount desc')
                    ->field('md.id, md.com_md_code as code, md.com_zip_name as name, count(follow.mdid) as fcount')
                    ->where("md.com_md_type01 != 4 or md.com_md_type01 is null")
                    ->select();
                $mdcount = M('follow')
                    ->table('wp_follow follow')
                    ->join('wp_com_md md on md.id = follow.mdid')
                    ->field('count(mdid) as count')
                    ->find();
                $this->assign('mdlist', $mdlist);
                $this->assign('mdcount', $mdcount['count']);
                $this->display();
            }
            else{
                $this->ajaxReturn(array('statu'=>1, 'content'=>'this mdcode is no permission to view '));
            }
        }
    }

    /*获取门店信息*/
    function getmdinfo(){
        $map['com_md_code'] = I('mdcode');
        $res = M('com_md')->where($map)->field('com_md_code as code, com_zip_name as name, url')->find();
        $this->ajaxReturn($res);
    }
//    function create2(){
//        $scene_str = I('scene_str');
//        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=a4miy5nMhRSsMX-NmuPyZbYrNNu7-1RvD5u9XqBvBprHk8q8arKV75-gH_uqc13-tU4hrcLjXMRv3-E5O2uTA4W6t9FEMxj2FICsevfuiNM";
//        $postdata['action_name'] = "QR_LIMIT_STR_SCENE";
//        $postdata['action_info'] = array('scene'=>array('scene_str'=>$scene_str));
//        $res = json_decode(JsonPost($url, json_encode($postdata)), true);
//        $this->ajaxReturn($res);
//    }

    /*导出门店拉粉数据*/
    function exportmdlist(){
        $smdcode = session('gy_mdcode');
        if(empty($smdcode)){
            $this->ajaxReturn('登录超时，请刷新页面!');
        }
        else{
            $mdinfo = M('com_md')->field('com_jc,com_zip,com_md_type01')->where(array('com_md_code'=>$smdcode))->find();
            if($mdinfo['com_md_type01'] ==3){
                $mdlist = M('com_md')
                    ->table('wp_com_md md')
                    ->join('LEFT JOIN wp_follow follow ON md.id = follow.mdid')
                    ->group('id')
                    ->order('fcount desc')
                    ->field('md.id, md.com_md_code as code, md.com_zip_name as name, count(follow.mdid) as fcount')
                    ->where("md.com_jc = '" . $mdinfo['com_jc'] . "' and (md.com_md_type01 != 4 or md.com_md_type01 is null)")
                    ->select();
            }
            else if($smdcode =='800000'){
                $mdlist = M('com_md')
                    ->table('wp_com_md md')
                    ->join('LEFT JOIN wp_follow follow ON md.id = follow.mdid')
                    ->group('id')
                    ->order('fcount desc')
                    ->field('md.id, md.com_md_code as code, md.com_zip_name as name, count(follow.mdid) as fcount')
                    ->where("md.com_md_type01 != 4 or md.com_md_type01 is null")
                    ->select();
            }
            else{
                $mdlist = array();
            }
            $str = "编号,店名,粉丝数\n";
            $str = iconv('utf-8','gb2312',$str);
            foreach($mdlist as $row){
                $mdcode = $row['code'];
                $mdname = iconv('utf-8','gb2312',$row['name']);
                $mdfcount = $row['fcount'];
                $str .= $mdcode.",".$mdname.",".$mdfcount.","."\n"; //用引文逗号分开
            }
            $filename = date("Y-m-d ", time()) . '门店拉粉统计.csv'; //设置文件名
            $this->export_csv($filename, $str); //导出
            exit;
        }
    }
    function export_csv($filename,$data) {
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo $data;

    }
}
